<?php
namespace Home\Controller;
use Think\Controller;

class IndexController extends Controller {
    public function index(){
        $weixin = new \Org\Weixin;
       // $weixin->getaccesstoken();
        $weixin->chushi();
    }
    public function wx(){
        $weixin = new \Org\Weixin;
         //$weixin->valid();
               // $weixin->receive();
        $weixin->responseMsg();

    }
    public function zhanshi(){
        $weixin = new \Org\Weixin;
        $code = $_GET['code'];
        $state = $_GET['state'];
        $url="";
        $userinfo = $weixin->jingmo($url, $code, $state);
        session('userinfo',$userinfo);
        if($code) {
            header("Location: $state");
        }else{
        }
    }
    public function responseMsg()
    {
        //get post data, May be due to the different environments
        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];

        //extract post data
        if (!empty($postStr)){
            /* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection,
               the best way is to check the validity of xml by yourself */
            libxml_disable_entity_loader(true);
            $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
            $fromUsername = $postObj->FromUserName;
            $toUsername = $postObj->ToUserName;
            $keyword = trim($postObj->Content);
            $time = time();
            $textTpl = "<xml>
							<ToUserName><![CDATA[%s]]></ToUserName>
							<FromUserName><![CDATA[%s]]></FromUserName>
							<CreateTime>%s</CreateTime>
							<MsgType><![CDATA[%s]]></MsgType>
							<Content><![CDATA[%s]]></Content>
							<FuncFlag>0</FuncFlag>
							</xml>";
            if(!empty( $keyword ))
            {
                $msgType = "text";
                $contentStr = "Welcome to wechat world!";
                $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                echo $resultStr;
            }else{
                echo "Input something...";
            }
        }else {
            echo "";
            exit;
        }
    }

    public function user(){

        getuserinfo();

    }



    public function indexjs(){

        $weixin = new \Org\Weixin;
       //echo  $weixin->getjsapi_ticket();
        $currenturl = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

        $sign = $weixin->getjssign();

        $this->assign('sign',$sign);


        $this->display();
    }

    public function qingchu(){

//        S("access_token",null);
//
//        S("js_ticket",null);
        session('userinfo',null);
    }

    public function pay()
    {

        $weixin = new \Org\Weixin;
        $arr = $weixin->getpaysign();
        $xml = "<xml>";
        foreach ($arr as $key => $val) {

            $xml .= "<" .$key.">".$val."</".$key.">";
        }
        $xml .= "</xml>";
        $url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
        $rest = $this->postxml($xml, $url);
        $fileContent = file_get_contents("php://input");

        $xmlResult = simplexml_load_string($fileContent);

       print_r($xmlResult);
    }

    function Post($curlPost,$url){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_NOBODY, true);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
        $return_str = curl_exec($curl);
        curl_close($curl);
        return $return_str;
    }

    function postxml($xml,$url){


        $ch = curl_init();
        $header[] = "Content-type: text/xml";//定义content-type为xml
        curl_setopt($ch, CURLOPT_URL, $url); //定义表单提交地址
        curl_setopt($ch, CURLOPT_POST, 1);   //定义提交类型 1：POST ；0：GET
        curl_setopt($ch, CURLOPT_HEADER, 1); //定义是否显示状态头 1：显示 ； 0：不显示
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);//定义请求类型
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);//定义是否直接输出返回流
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //定义提交的数据，这里是XML文件

        curl_exec($curl);
        curl_close($ch);//关闭
    }

    public function jiazai(){


        $info = M('Info');

        $list  = $info->where('id>0')->limit(0,5)->select();
        $this->assign('list',$list);
        $this->display();
    }

    public function ajax(){
        $info = M('Info');
        $cout = count($info);
        $page = intval($_GET['page']);  //获取请求的页数
        $start = $page*4;
        $list  = $info->where('id>0')->limit($start,15)->select();
        echo json_encode($list);


    }
}